#!/usr/bin/perl

my $cm0=(stat$0)[9];

chdir '/opt/down/done/upd/inc';
use Digest::SHA1;
use String::CRC32;
my $sha1 = Digest::SHA1->new;
my $cache='sums.lst';
my $cm1=(stat'.')[9];
my $cm2=(stat$cache)[9];
$cm1=$cm0 if $cm0>$cm1;

sub validate_ini($)
{
    open S,'<'.$_[0] or return 0;
    binmode S;
    my $sum=crc32(*S,-1) ^ -1;
    seek(S,0,0);
    $sha1->addfile(*S);
    close(S);
    return $sum;
}

if ($cm1>$cm2)
{
print "X-Recreating: $cache:$cm1,$cm2\n";
open OUT,'>'.$cache;
print OUT "#mod16,size,crc,sha1,name\n";

while (<*.{upd,gem}>)
{
    my ($size,$mt)=(stat$_)[7,9];
    my $sum=validate_ini($_);
    printf OUT "%08x,%06u,%08x,%s,%s\n",$mt,$size,$sum,$sha1->hexdigest,$_;
}

close OUT;
}

print <<EOF;
Status: 301 Moved
Location: http://mine/down/upd/inc/$cache

EOF
